package com.bzs.batch.reconcile.batch.myprocessor;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.bzs.batch.reconcile.entity.HostFile;
import com.bzs.batch.reconcile.entity.SysTransactionflow;
import com.bzs.batch.reconcile.mapper.SysTransactionflowMapper;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;

/**
 * @Classname Processer
 * @Author baozs
 * @Date 2021/12/29 14:12
 * @Version 1.0
 * @Description
 **/
@Configuration
@AllArgsConstructor
public class MyProcessor {
    private final SysTransactionflowMapper sysTransactionflowMapper;

    public ItemProcessor<? super HostFile, ? extends HostFile> getMyProcessor(@Value("#{jobParameters[date]}") String date) {
        return (ItemProcessor<HostFile, HostFile>) item -> {

            QueryWrapper<SysTransactionflow> queryWrapper = new QueryWrapper<>();
            String allSerialno = item.getAllSerialno();
            if (!StringUtils.isNotEmpty(allSerialno)) {
                System.out.println("全局流水号有空");
                return item;
            }
            queryWrapper.eq("host_serialno", allSerialno);
            SysTransactionflow sysTransactionflow = sysTransactionflowMapper.selectOne(queryWrapper);
            //比较相应的流水
            boolean isOk = compareTrans(item, sysTransactionflow);
            UpdateWrapper<SysTransactionflow> updateWrapper = new UpdateWrapper<>();
            //如果值不为空
            if (StringUtils.isNotEmpty(item.getDebitAccount()) && StringUtils.isNotEmpty(item.getCredit()) && StringUtils.isNotEmpty(item.getHostTradeResult()) && StringUtils.isNotEmpty(item.getTransAmount())) {
                if (!isOk) {
                    //对账失败，更改sysTransactionflow表里的数据，并打印日志。
                    updateWrapper.eq("host_serialno", allSerialno);
                    updateWrapper.set("debit_account", item.getDebitAccount());//借方账户
                    updateWrapper.set("credit_account", item.getCredit());//贷方账户
                    updateWrapper.set("trade_result", item.getHostTradeResult());//交易结果
                    updateWrapper.set("trans_amount", item.getTransAmount());//交易金额
                }
                //设置对账状态
                updateWrapper.set("reconcil_result", "01");
                int update = sysTransactionflowMapper.update(new SysTransactionflow(), updateWrapper);
                if (update > 0) {
                    System.out.println("流水更新成功！流水号：" + allSerialno);
                }
            } else {//发现空值，对账状态更新为03--状态异常
                updateWrapper.eq("host_serialno", allSerialno);
                updateWrapper.set("reconcil_result", "03");
                int update = sysTransactionflowMapper.update(new SysTransactionflow(), updateWrapper);
                if (update > 0) {
                    System.out.println("流水号：" + allSerialno + "状态异常");
                }
            }
            return item;
        };
    }

    private boolean compareTrans(HostFile item, SysTransactionflow sysTransactionflow) {
        //比较：借方账号$贷方账号$交易结果$交易金额
        boolean debitAccount = item.getDebitAccount().equals(sysTransactionflow.getDebitAccount());
        boolean credit = item.getCredit().equals(sysTransactionflow.getCreditAccount());
        boolean hostTradeResult = item.getHostTradeResult().equals(sysTransactionflow.getTradeResult());
        boolean transAmount = item.getTransAmount().equals(sysTransactionflow.getTransAmount());
        if (debitAccount && credit && hostTradeResult && transAmount) {
            System.out.println("对账成功");
            return true;
        } else {
            System.out.println("对账失败，流水号: " + item.getAllSerialno());
            return false;
        }
    }
}
